<?php
require_once("mysql.inc.php");

if(isset($_GET["query"])) {
	searchBook($_GET["query"]);
}

function searchBook($query) {

	$query = mysql_real_escape_string($query);
	
	$mysqlQuery = "SELECT * FROM `books` WHERE `title` LIKE '%$query%' OR `id` LIKE '%$query%' OR `author` LIKE '%$query%' 
				   OR `genre` LIKE '%$query%' OR `publisher` LIKE '%$query%' ORDER BY `author` LIMIT 20";
	$result = mysql_query($mysqlQuery) or die(mysql_error());
	$num = mysql_num_rows($result);
	
	$i = 1;
	$no_info = "Niet bekend";
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($num));
	$root->appendChild($n);
	
	while ($fetch = mysql_fetch_array($result)) {
		$t = $doc->createElement("book$i");
		
		$title = $doc->createElement("title$i");
		if(!empty($fetch["title"])) {
			$title->appendChild($doc->createTextNode($fetch["title"]));
		}			
		else {
			$title->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($title);
		
		$author = $doc->createElement("author$i");
		if(!empty($fetch["author"])) {
			$author->appendChild($doc->createTextNode($fetch["author"]));
		}
		else {
			$author->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($author);
		
		$author2 = $doc->createElement("author2_$i");
		if(!empty($fetch["author2"])) {
			$author2->appendChild($doc->createTextNode($fetch["author2"]));
		}
		else {
			$author2->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($author2);
		
		$genre = $doc->createElement("genre$i");
		if(!empty($fetch["genre"])) {
			$genre->appendChild($doc->createTextNode($fetch["genre"]));
			}
		else {
			$genre->appendChild($doc->createTextNode($no_info));
			}	
		$t->appendChild($genre);
		
		$theme = $doc->createElement("theme1_$i");
		if(!empty($fetch["theme1"])) {
			$theme->appendChild($doc->createTextNode($fetch["theme1"]));
		}
		else {
			$theme->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($theme);
		
		$theme2 = $doc->createElement("theme2_$i");
		if(!empty($fetch["theme2"])) {
			$theme2->appendChild($doc->createTextNode($fetch["theme2"]));	
		}
		else {
			$theme2->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($theme2);
		
		$price = $doc->createElement("price$i");
		if(!empty($fetch["price"])) {
			$price->appendChild($doc->createTextNode($fetch["price"]));	
		}
		else {
			$price->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($price);
		
		$age = $doc->createElement("age$i");
		if(!empty($fetch["age"])) {
			$age->appendChild($doc->createTextNode($fetch["age"]));	
		}
		else {
			$age->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($age);
		
		$publisher = $doc->createElement("publish$i");
		if(!empty($fetch["publisher"])) {
			$publisher->appendChild($doc->createTextNode($fetch["publisher"]));	
		}
		else {
			$publisher->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($publisher);
		
		$place = $doc->createElement("place$i");
		if(!empty($fetch["place"])) {
			$place->appendChild($doc->createTextNode($fetch["place"]));	
		}
		else {
			$place->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($place);
		
		$material = $doc->createElement("material$i");
		if(!empty($fetch["material"])) {
			$material->appendChild($doc->createTextNode($fetch["material"]));	
		}
		else {
			$material->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($material);
		
		$avaible = $doc->createElement("avaible$i");
		if(!empty($fetch["avaible"])) {
			$avaible->appendChild($doc->createTextNode($fetch["avaible"]));	
		}
		else {
			$avaible->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($avaible);
		
		$likes = $doc->createElement("likes$i");
		if(!empty($fetch["likes"])) {
			$likes->appendChild($doc->createTextNode($fetch["likes"]));
		}
		else {
			$likes->appendChild($doc->createTextNode("0"));
		}
		$t->appendChild($likes);
		
		$info = $doc->createElement("info$i");
		if(!empty($fetch["info"])) {
			$info->appendChild($doc->createTextNode($fetch["info"]));
		}
		else {
			$info->appendChild($doc->createTextNode($fetch["title"] . "-" . $fetch["author"]));
		}
		$t->appendChild($info);
		
		$id = $doc->createElement("id$i");
		$id->appendChild($doc->createTextNode($fetch["id"]));
		$t->appendChild($id);
				
		$root->appendChild($t);
		
		$i++;	
	}
	
	echo $doc->saveXML();
}
?>